//
//  JHNetWorkTools.swift
//  OC中的单例的封装
//
//  Created by apple on 16/5/26.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import AFNetworking
//声明枚举类型
enum HTTPMethods:Int {
    case GET
    case POST
}
struct API {
    static let hostName = "https://api.weibo.com/"
    //加载用户的token
    static let loadAccessToken = "oauth2/access_token"
    //加载用户的信息
    static let loadUserInfo = "2/users/show.json"
    //加载首页数据
    static let loadHomeData = "2/statuses/home_timeline.json"
}
class JHNetWorkTools: AFHTTPSessionManager {

    static let sharedTool:JHNetWorkTools = {
        
        let url = NSURL(string:API.hostName)
    let tools = JHNetWorkTools (baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
    return tools
    }()
    
/*
tools.GET(<#T##URLString: String##String#>, parameters: <#T##AnyObject?#>, success: <#T##((NSURLSessionDataTask, AnyObject?) -> Void)?##((NSURLSessionDataTask, AnyObject?) -> Void)?##(NSURLSessionDataTask, AnyObject?) -> Void#>, failure: <#T##((NSURLSessionDataTask?, NSError) -> Void)?##((NSURLSessionDataTask?, NSError) -> Void)?##(NSURLSessionDataTask?, NSError) -> Void#>)

*/

        func request(method:HTTPMethods,urlString:String,parameters:AnyObject?,finished:(responseObject:AnyObject?,error:NSError?) -> ()  ){
            
            let success = {(task:NSURLSessionDataTask, result:AnyObject?) -> ()
                in
                finished(responseObject:result ,error:nil)
            }
            let failure = {(task:NSURLSessionDataTask?,error: NSError) -> ()
                in
                //执行失败的回调
                print(error)
                finished(responseObject:nil ,error:error)
            }

        if method == .GET {
            GET(urlString, parameters: parameters, success: success, failure: failure)
            
            
//            GET(urlString, parameters: parameters, success: { (_, result) in
//                //_表示忽略
//                //执行请求成功的回调
//                finished(responseObject:result ,error:nil)
//                }, failure: { (_, error) in
//                    //执行失败的回调
//                    finished(responseObject:nil ,error:error)
//            })
//        }else{
//            POST(urlString, parameters: parameters, success: { (_, result) in
//                //_表示忽略
//                //执行请求成功的回调
//                finished(responseObject:result ,error:nil)
//                }, failure: { (_, error) in
//                    //执行失败的回调
//                    finished(responseObject:nil ,error:error)
//            })

        }else{
            POST(urlString, parameters: parameters, success: success, failure: failure)
            }
    
    }
}
//
extension JHNetWorkTools{
    //请求用户的token
    func loadAccessToken(code:String,finished:(responseObject: AnyObject?, error: NSError?)->()){
    let parameters = ["client_id" :client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        // 调用核心方法
request(.POST, urlString: API.loadAccessToken, parameters: parameters, finished: finished)
    
    }
    //加载用户信息
    func loadUserInfo(response:[String:AnyObject],finished:(responseObject: AnyObject?, error: NSError?)->()){
        //用户访问接口权限的标识
        let token = response["access_token"]!
        let uid = response["uid"]!
        let parameters = ["access_token":token,"uid":uid]
        request(.GET, urlString: API.loadUserInfo, parameters: parameters, finished: finished)
    
    
    }
    

}
//首页相关的分类
extension JHNetWorkTools{
    func loadHomePageData(finished: (responseObject: AnyObject?, error: NSError?) -> ()){
    guard let token = JHUserAccountModel.sharedAccountViewModel.userAccount?.access_token else{
    return
    }
let parameters = ["access_token":token]
request(.GET, urlString: API.loadHomeData, parameters: parameters, finished: finished)
   }
}
